﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Runtime.Remoting.Messaging;

namespace myUserManagement
{
    public class ghieUserManagement
    {

        OdbcConnection oc = new OdbcConnection();
        OdbcDataAdapter oa = new OdbcDataAdapter();
        public DataTable tbl = new DataTable();
        int i = 0;

        public void connection()
        {
            oc.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};" +
                    "Server=localhost;" +
                    "Database=ghiedatabase;" +
                    "UID=root;" +
                    "PWD=12345";
            oc.Open();
        }

        public void DBClose()
        {
            oc.Close();
        }


        #region login
        //async sa login
        delegate void myloginThread(string login);
        public void loginOperation(string login)
        {

            myloginThread mlt = new myloginThread(mylogin);
            AsyncCallback ac = new AsyncCallback(CallBackMethod);
            IAsyncResult ar = mlt.BeginInvoke(login, ac, null);
        }

        void CallBackMethod(IAsyncResult ar)
        {

            myloginThread mlt = (myloginThread)((AsyncResult)ar).AsyncDelegate;
            mlt.EndInvoke(ar);
        }

        public void mylogin(string login)
        {
            tbl = new DataTable();
            oa.SelectCommand = new OdbcCommand(login, oc);
            tbl.Clear();
            tbl.Reset();
            oa.Fill(tbl);

        }
        #endregion

        #region loaddata
        delegate void loaddataThread(string load);
        public void loaddataOperation(string load)
        {

            myloginThread ldt = new myloginThread(loadData);
            AsyncCallback ac = new AsyncCallback(LDCallBackMethod);
            IAsyncResult ar = ldt.BeginInvoke(load, ac, null);
        }
        public int AsyncComplete = 0;
        void LDCallBackMethod(IAsyncResult ar)
        {

            loaddataThread ldt = (loaddataThread)((AsyncResult)ar).AsyncDelegate;
            ldt.EndInvoke(ar);
        }

        public void loadData(string load)
        {
            tbl = new DataTable();
            tbl.Clear();
            tbl.Reset();
            oa.SelectCommand = new OdbcCommand(load, oc);
            oa.Fill(tbl);
        }
        #endregion

        #region addData
        delegate void addDataThread(string add);
        public void addDataOperation(string add)
        {

            addDataThread adt = new addDataThread(AddData);
            AsyncCallback ac = new AsyncCallback(addDataCallBackMethod);
            IAsyncResult ar = adt.BeginInvoke(add, ac, null);
        }

        void addDataCallBackMethod(IAsyncResult ar)
        {

            addDataThread adt = (addDataThread)((AsyncResult)ar).AsyncDelegate;
            adt.EndInvoke(ar);
        }
        public void AddData(string add)
        {
            oa.SelectCommand = new OdbcCommand(add, oc);
            oa.Fill(tbl);

        }
        #endregion

        #region updatepassword
        delegate void updatePassThread(string update);
        public void updatePassOperation(string update)
        {

            updatePassThread upt = new updatePassThread(UpdatePassword);
            AsyncCallback ac = new AsyncCallback(updatePassCallBackMethod);
            IAsyncResult ar = upt.BeginInvoke(update, ac, null);
        }

        void updatePassCallBackMethod(IAsyncResult ar)
        {

            updatePassThread upt = (updatePassThread)((AsyncResult)ar).AsyncDelegate;
            upt.EndInvoke(ar);

        }

        public void UpdatePassword(string update)
        {
            try
            {
                oa.SelectCommand = new OdbcCommand(update, oc);
                oa.Fill(tbl);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }
        #endregion

    }
}